package org.panda.tcmoc.view;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;
import org.panda.tcmoc.domain.Session;
import org.panda.tcmoc.service.SpProductService;
import org.panda.tcmoc.service.impl.SpProductServiceImpl;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;

public class MonitorViewPart extends ViewPart {

	public static final String ID = "org.panda.tcmoc.view.MonitorViewPart"; //$NON-NLS-1$
	private Text text_sp_approval;
	private SpProductService service = new SpProductServiceImpl();
	private Text text_sp_repair;

	public MonitorViewPart() {
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		{
			Label lblUserId = new Label(container, SWT.NONE);
			lblUserId.setBounds(36, 26, 54, 12);
			lblUserId.setText("User ID");
		}

		Label label = new Label(container, SWT.NONE);
		label.setBounds(119, 26, 54, 12);

		createActions();
		initializeToolBar();
		initializeMenu();
		label.setText(Session.getInstance().getUser().getId());

		Group grpSp = new Group(container, SWT.NONE);
		grpSp.setText("SP");
		grpSp.setBounds(0, 57, 243, 73);

		Label lblRepairQueue = new Label(grpSp, SWT.NONE);
		lblRepairQueue.setLocation(10, 27);
		lblRepairQueue.setSize(137, 12);
		lblRepairQueue.setText("Repair Queue");

		text_sp_repair = new Text(grpSp, SWT.BORDER);
		text_sp_repair.setEnabled(false);
		text_sp_repair.setBounds(151, 24, 41, 18);

		Label lblApprovalQueue = new Label(grpSp, SWT.NONE);
		lblApprovalQueue.setLocation(10, 51);
		lblApprovalQueue.setSize(137, 12);
		lblApprovalQueue.setText("Approval Queue");

		text_sp_approval = new Text(grpSp, SWT.BORDER);
		text_sp_approval.setLocation(151, 45);
		text_sp_approval.setSize(41, 18);
		text_sp_approval.setEnabled(false);

		text_sp_approval.setText(String.valueOf(service
				.findUnapproveTransactions().size()));
		text_sp_repair.setText(String.valueOf(service
				.findUnrepairTransactions().size()));

		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.setBounds(119, 216, 72, 22);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_sp_approval.setText(String.valueOf(service
						.findUnapproveTransactions().size()));
				text_sp_repair.setText(String.valueOf(service
						.findUnrepairTransactions().size()));
			}
		});
		btnNewButton.setText("\u5237\u65B0");

	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
}
